package com.yex.controller.modify.order;

import com.baomidou.mybatisplus.plugins.Page;
import com.yex.common.exception.RRException;
import com.yex.common.utils.R;
import com.yex.entity.UserOrderEntity;
import com.yex.entity.vo.UserOrderVo;
import com.yex.service.modify.order.SmsService;
import com.yex.service.modify.order.UserOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/appApi/userOrder")
public class UserOrderController {

    @Autowired
    private UserOrderService userOrderService;

    @Autowired
    private SmsService smsService;

    @GetMapping("pageListByUser")
    public R pageListByUser(Integer orderType, Integer status, Integer page, Integer rows) {
        Page<UserOrderVo> data = userOrderService.pageListByUser(orderType, status, page, rows);
        return R.okData(data);
    }

    @GetMapping("selectOrderListByMerchant")
    public R selectOrderListByMerchant(Integer orderType, Integer status, Integer page, Integer rows) {
        Page<UserOrderVo> data = userOrderService.pageListByMerchant(orderType, status, page, rows);
        return R.okData(data);
    }

    @PostMapping("createOrder")
    public R createOrder(Integer orderType, String moneyPassword, UserOrderEntity userOrderEntity) {
        if (userOrderEntity.getOrderId() == null) throw new RRException("缺少必填参数: orderId");
        if (StringUtils.isBlank(moneyPassword)) throw new RRException("请输入支付密码");
        userOrderEntity = userOrderService.createOrder(orderType, moneyPassword, userOrderEntity);
        return R.okData(userOrderEntity);
    }

    @PostMapping("confirmPayByUser")
    public R confirmSellerPay(Integer userOrderId, String moneyPassword) {
        userOrderService.confirmPayByUser(userOrderId, moneyPassword);
        return R.okData("支付完成");
    }

    @PostMapping("confirmPayByMerchant")
    public R confirmBuyPay(Integer userOrderId, String moneyPassword) {
        userOrderService.confirmPayByMerchant(userOrderId, moneyPassword);
        return R.okData("支付完成");
    }

    @PostMapping("confirmOrderByUser")
    public R confirmOrderByUser(Integer userOrderId, String moneyPassword) {
        if (userOrderId == null) throw new RRException("缺少参数: 用户订单id");
        if (StringUtils.isBlank(moneyPassword)) throw new RRException("请输入支付密码");
        userOrderService.confirmOrderByUser(userOrderId, moneyPassword);
        return R.okData("订单完成");
    }

    @PostMapping("confirmOrderByMerchant")
    public R confirmOrderByMerchant(Integer userOrderId, String moneyPassword) {
        if (userOrderId == null) throw new RRException("缺少参数: 用户订单id");
        if (StringUtils.isBlank(moneyPassword)) throw new RRException("请输入支付密码");
        userOrderService.confirmOrderByMerchant(userOrderId, moneyPassword);
        return R.okData("订单完成");
    }

    @PostMapping("cancelOrder")
    public R cancelOrder(Integer id) {
        if (id == null) throw new RRException("缺少参数: id");
        userOrderService.cancelOrder(id);
        return R.ok("撤销订单成功");
    }

    @GetMapping("findById")
    public R findById(Integer userOrderId) {
        UserOrderEntity userOrderEntity = userOrderService.findRelationData(userOrderId);
        return R.okData(userOrderEntity);
    }

    public void sendMsm(Integer userOrderId, Integer smsType) {

    }

}
